local shanjia = fk.CreateSkill{
  name = "iberia__shanjia",
}

local choices = {"#iberia__shanjia-use", "#iberia__shanjia-recast"}

Fk:loadTranslationTable {
  ["iberia__shanjia"] = "缮甲",
  [":iberia__shanjia"] = "出牌阶段限一次，或当你受到伤害后，你可以使用或重铸一张装备牌，然后可以弃置攻击范围内一名角色的一张牌。",
  ["#iberia__shanjia"] = "缮甲：可以使用或重铸一张装备牌，然后可以弃置攻击范围内一名角色的一张牌。",
  ["#iberia__shanjia-dis"] = "缮甲：可以弃置攻击范围内一名角色的一张牌",

  [choices[1]] = "使用装备牌",
  [choices[2]] = "重铸装备牌",
}

shanjia:addEffect("active", {
  anim_type = "control",
  prompt = "#iberia__shanjia",
  handly_pile = true,
  can_use = function (self, player)
    return player:usedSkillTimes(shanjia.name, Player.HistoryPhase) < 1
  end,
  interaction = function (self)
    return UI.ComboBox { choices = choices }
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected < 1 and Fk:getCardById(to_select).type == Card.TypeEquip
  end,
  feasible = function (self, player, _, selected_cards)
    if #selected_cards ~= 1 then return false end
    if self.interaction.data == choices[1] then
      return table.contains(player:getCardIds("h&"), selected_cards[1])
    else
      return true
    end
  end,
  on_use = function (self, room, effect)
    local player = effect.from
    if self.interaction.data == choices[1] then
      room:useCard{
        from = player,
        tos = { player },
        card = Fk:getCardById(effect.cards[1]),
      }
    else
      room:recastCard(effect.cards, player, shanjia.name)
    end

    if player.dead then return end
    local to = room:askToChoosePlayers(player, {
      targets = table.filter(room.alive_players, function (p)
        return player:inMyAttackRange(p)
      end),
      max_num = 1,
      min_num = 1,
      skill_name = shanjia.name,
      cancelable = true,
      prompt = "#iberia__shanjia-dis",
    })

    if #to == 0 then return end
    local card = room:askToChooseCard(player, {
      target = to[1],
      flag = "he",
      skill_name = shanjia.name,
      prompt = shanjia.name,
    })

    room:throwCard(card, shanjia.name, to[1], player)
  end
})

shanjia:addEffect(fk.Damaged, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(shanjia.name) and player == target
  end,
  on_trigger = function (self, event, target, player, data)
    player.room:askToUseActiveSkill(player, {
      skill_name = shanjia.name,
      prompt = "#iberia__shanjia",
    })
  end,
})

return shanjia